/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2022-06-07
 * Time: 11:06
 */
public class binarySearchTree {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;


    public void inOder(TreeNode root) {

        if(root == null) return;

        inOder(root.left);
        System.out.print(root.key +" ");
        inOder(root.right);
    }

    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);

        if(root == null) {
            root = node;
            return true;
        }

        TreeNode cur = root;
        TreeNode prev = null;
        while(cur != null) {

            if(key < cur.key) {
                prev = cur;
                cur = cur.left;
            } else if(key > cur.key) {
                prev = cur;
                cur = cur.right;
            } else {
                return false;
            }
        }

        if(key < prev.key) {
            prev.left = node;
        } else {
            prev.right = node;
        }
        return true;
    }

    public TreeNode search(int key) {
        TreeNode cur = root;

        while(cur != null) {
            if(key > cur.key) {
                cur = cur.right;
            }else if(key < cur.key){
                cur = cur.left;
            }else {
                return cur;
            }
        }
        return null;
    }

    public void remove(int key) {
        TreeNode cur = root;
        TreeNode prev = null;

        while(cur != null) {
            if(key > cur.key) {
                prev = cur;
                cur = cur.right;
            } else if(key < cur.key) {
                prev = cur;
                cur = cur.left;
            } else {
                removeNode(prev,cur);
                return;
            }
        }
    }

    private void removeNode(TreeNode prev,TreeNode cur) {
        if(cur.left == null) {
            if(root == cur) {
                root = cur.right;
            }else if(prev.left == cur) {
                prev.left = cur.right;
            } else if(prev.right == cur){
                prev.right = cur.right;
            }
        }else if(cur.right == null) {
            if(root == cur) {
                root = cur.left;
            }else if(prev.left == cur) {
                prev.left = cur.left;
            } else if(prev.right == cur){
                prev.right = cur.left;
            }
        }else {
            TreeNode parentget = cur;
            TreeNode parent = cur.right;

            while(parent.left != null) {
                parentget = parent;
                parent = parent.left;
            }

            cur.key = parent.key;

            if(cur == parentget.left) {
                parentget.left = parent.right;
            } else {
                parentget.right = parent.right;
            }

        }
    }
}
